package asteroids.model.statements;

import java.util.HashMap;
import java.util.Map;

import asteroids.model.*;
import asteroids.model.expressions.*;
import asteroids.model.types.T;

/**
 * A class that represents an assignment statement (S) 
 * 
 * @version 1.0
 * 
 * @author	Thijs Peirelinck & Wouter Parys
 *
 */
public class Assignment extends BasicStatement{
	
	private String variable;
	public Assignment(String variable, E rhs) {
		super(rhs);
		this.variable = variable;
	}
	
	public void setExecuter(Ship executer) {
		getExpression().setExecuter(executer);
	}

	@Override
	public void doStatement(HashMap<String, E> variables) {
		variables.put(variable, getValidatedExpressionCopy(getExpression(), variables));	
	}
	
	@Override
	public boolean typeCheck(Map<String, T> globals) {
		if(globals.get(variable) == null)
			return false;
		if(getExpression() instanceof Variable) {
			if(globals.get(getExpression().getValue()) == null)
				return false;
			else 
				return globals.get(variable).getReturnType().equals(globals.get(getExpression().getValue()).getReturnType());
		}
		else 
			return (globals.get(variable).getReturnType().equals(getExpression().getReturnType())) && (getExpression().typeCheck(globals));
	}
}
